﻿
namespace Kazoo
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GotXtraCC;
using Mischief;

using System.Net;
using System.Runtime.CompilerServices;

using Yahoo;
using Yahoo.Ymsg;
using System.Security.Permissions;

    public partial class Form3 : Form
    {
        private delegate void ButtonGrey();
        private delegate void Tablist(int tab);
        private delegate void TimerStart();
        private delegate void Userlist(string user);
        private Userlist ds;
        private Userlist dss;
        private Tablist dss2;
        private Userlist dssd;
        private Userlist dssd3;
        private Tablist level2;
        private Tablist level3;
        private ButtonGrey bgrey;
        private TimerStart ts;
        YMVCHook.PacketHook p;

        #region Constructor
        public Form3()
        {
            InitializeComponent();
            p = new YMVCHook.PacketHook();
            p.HookedRoomEvent += new YMVCHook.PacketHook.HookedRoomInfo(p_HookedEvent);
            p.HookedUserNameEvent+=new YMVCHook.PacketHook.HookedUserInfo(p_HookedUserNameEvent);
            ds = new Userlist(adduser);
            dss = new Userlist(deluser);
            dssd = new Userlist(keyuser);
            dss2 = new Tablist(changetab);
            dssd3 = new Userlist(updateStatus);
            level = new Tablist(leveler);
            level2 = new Tablist(levelchanger);
            level3 = new Tablist(leveler3);
            bgrey = new ButtonGrey(denable);
            ts = new TimerStart(timerstart);
            keyed = false;
            Joinattempts = 0;
            listView1.ItemCheck += new ItemCheckEventHandler(listView1_ItemCheck);
        }
        #endregion

        
        void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if(e.NewValue == CheckState.Checked)
            {
                wd.ignoreuser(listView1.Items[e.Index].Text);
            }
            else
            {
                wd.unignoreuser(listView1.Items[e.Index].Text);
            }
        }
        //public void initvd(VoiceData vd)
        //{
        //    //if(System.IO.File.Exists("datafile2.soap"))
        //    //{
        //    //    System.IO.FileStream stream = new System.IO.FileStream("DataFile2.soap", System.IO.FileMode.Open);
        //    //    SoapFormatter dsds = new SoapFormatter();
        //    //    VoiceData vd = (VoiceData)dsds.Deserialize(stream);
        //    //    stream.Close();
        //    //    dsds = null;
        //    //    System.IO.File.Delete("datafile2.soap");
        //        statusStrip1.Invoke(dssd3, new object[] { vd.RoomName });
        //        wd = new VoiceStream(vd.UserName, vd.RoomName, vd.RoomSpace, vd.VcCookie, Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
        //        wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
        //        wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
        //        wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
        //        wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
        //        wd.Listners1 += new VoiceStream.VCEventHandler3(vss_Listners1);
        //        tabControl1.Invoke(dss2, new object[] { 1 });
                
        //    //}
        //}

        private void adduser(string data)
        {
            listView1.Items.Add(data, data, 0);
        }

        void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length > 0)
            {
                client = new YmsgClient();
                client.Error += new ChatEventHandler(client_Error);
                client.VCCookie += new ChatEventHandler(client_Other);
                if (client.Login(textBox1.Text, textBox2.Text).Length > 0)
                    statusStrip1.Invoke(dssd3, new object[] { "Logged In" });
            }
            else
            {
                statusStrip1.Invoke(dssd3, new object[] { "Type A Password" });
            }
        }

        void client_Error(object sender, ChatEventArgs e)
        {
            //if(e.ID!="155")
            //statusStrip1.Invoke(dssd3, new object[] { e.ID });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        void levelchanger(int i)
        {
            if(i == 0)
            xtraProgressBar1.BarColor1 = Color.MistyRose;
        else
            xtraProgressBar1.BarColor1 = Color.WhiteSmoke;
        }

        private void button3_Click(object sender, EventArgs e)
        {

                if (!keyed)
                {
                    
                }
                else
                {
                    
                }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (p.username != null)
            {
                string fd = p.username;
                p = new YMVCHook.PacketHook();
                p.HookedRoomEvent += new YMVCHook.PacketHook.HookedRoomInfo(p_HookedEvent);
                p.HookedUserNameEvent += new YMVCHook.PacketHook.HookedUserInfo(p_HookedUserNameEvent);
                p.username = fd;
                statusStrip1.Invoke(dssd3, new object[] { fd + " Join A Room" });
                Hook.Enabled = false;
                p.start();
            }
            else
            {
                statusStrip1.Invoke(dssd3, new object[] { "Connect Client/Join Chat" });
                Hook.Enabled = false;
                p.start();
            }
        }

        void p_HookedUserNameEvent(string username)
        {
            statusStrip1.Invoke(dssd3, new object[] { username + " join a room" }); 
        }

        void p_HookedEvent(string username, string roomname, string roomspace, string roomkey)
        {
            if (username != "" && roomname != "" && roomspace != "" && roomkey != "")
            {
                globaluser = username;
                //System.Windows.Forms.MessageBox.Show(username+"-"+roomname+"-"+roomspace+"-"+roomkey);
                Hook.Invoke(bgrey);
                timer1.Stop();
                statusStrip1.Invoke(dssd3, new object[] { "Obtaining VC: " + roomname });
                globaluser = username;
                if (wd != null)
                {
                    wd.Dispose();
                    wd = null;
                }
                wd = new VoiceStream(username, roomname, roomspace, roomkey, Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
                wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
                wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
                wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
                wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
                wd.Listners1 += new VoiceStream.VCEventHandler3(vss_Listners1);
                wd.VCConnected += new VoiceStream.VCEventHandler3(wd_VCConnected);
                wd.Connect(true);
                this.Invoke(ts);
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            try
            {

                if (wd != null)
                {
                    wd.vc_logout();
                    wd = null;
                }
            }
            catch (Exception e2)
            {

            }
            tabControl1.Invoke(dss2, new object[] { 0 });
            statusStrip1.Invoke(dssd3, new object[] { "Left Room" });
        }

        private void button5_Click(object sender, EventArgs e)
        {
            VoiceStream.mute = !VoiceStream.mute;
        }

        private void changetab(int i)
        {
            tabControl1.SelectTab(i);
            Joinattempts = 0;
        }

        private void client_Other(object sender, ChatEventArgs e)
        {
            if (client.UserName != "" && client.RoomName != "" && e.ID != "" && e.Data.ToString() != "")
            {
                Joinattempts = 0;
                timer1.Stop();
                statusStrip1.Invoke(dssd3, new object[] { "Obtaining VC: " + client.RoomName });
                if (wd != null)
                {
                    wd.Dispose();
                    wd = null;
                }
                wd = new VoiceStream(client.UserName, client.RoomName, e.ID, e.Data.ToString(), Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
                wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
                wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
                wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
                wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
                wd.Listners1 += new VoiceStream.VCEventHandler3(vss_Listners1);
                wd.VCConnected += new VoiceStream.VCEventHandler3(wd_VCConnected);
                wd.Connect(true);
                this.Invoke(ts);
            }
        }

        void wd_VCConnected(string data)
        {
            timer2.Stop();
            tabControl1.Invoke(dss2, new object[] { 1 });
            statusStrip1.Invoke(dssd3, new object[] { "" });
        }

        void vss_Listners1(string data)
        {
            button3.Invoke(level3, Convert.ToInt32(data));
        }

        private void deluser(string data)
        {
            listView1.Items.RemoveByKey(data);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            if (wd != null)
            {
                wd.vc_logout();
                wd = null;
            }
            base.OnClosing(e);
        }
        protected override void Dispose(bool disposing)
        {
            try
            {
                BottomToolStripPanel.Dispose();
                Hook.Dispose();
                button1.Dispose();
                button2.Dispose();
                button3.Dispose();
                button4.Dispose();
                button5.Dispose();
                client.Dispose();
                columnHeader1.Dispose();
                ContentPanel.Dispose();
                contextMenuStrip1.Dispose();
                ignoreToolStripMenuItem.Dispose();
                LeftToolStripPanel.Dispose();
                listView1.Dispose();
                RightToolStripPanel.Dispose();
                statusStrip1.Dispose();
                tabControl1.Dispose();
                tabPage1.Dispose();
                tabPage2.Dispose();
                textBox1.Dispose();
                textBox2.Dispose();
                textBox3.Dispose();
                timer1.Dispose();
                toolStripStatusLabel1.Dispose();
                TopToolStripPanel.Dispose();
                unignoreToolStripMenuItem.Dispose();
                xtraProgressBar1.Dispose();
                BottomToolStripPanel = null;
                button1 = null;
                button2 = null;
                button3 = null;
                button4 = null;
                button5 = null;
                client = null;
                columnHeader1 = null;
                ContentPanel = null;
                contextMenuStrip1 = null;
                ds = null;
                dss = null;
                dss2 = null;
                dssd = null;
                dssd3 = null;
                level3 = null;
                ignoreToolStripMenuItem = null;
                LeftToolStripPanel = null;
                level = null;
                listView1 = null;
                RightToolStripPanel = null;
                statusStrip1 = null;
                tabControl1 = null;
                tabPage1 = null;
                tabPage2 = null;
                textBox1 = null;
                textBox2 = null;
                textBox3 = null;
                timer1 = null;
                toolStripStatusLabel1 = null;
                TopToolStripPanel = null;
                unignoreToolStripMenuItem = null;
                wd = null;
                xtraProgressBar1 = null;
                AddUser = null;
                DelUser = null;
            }
            catch (Exception)
            {

            }
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            GC.Collect();
            base.Dispose(disposing);
        }

        private void ignoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.SelectedItems[0].Checked = true;
            wd.ignoreuser(listView1.SelectedItems[0].Text);
        }
        private void denable()
        {
            Hook.Enabled = !(Hook.Enabled);
        }
        private void keyuser(string data)
        {
            xtraProgressBar1.TextBegin = data + " ";
        }

        private void leveler(int i)
        {
            xtraProgressBar1.BarValue = Convert.ToInt32((double)(((double)i) / 2.5));
        }

        private void leveler3(int i)
        {
            button3.Text = Convert.ToString(i); ;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Joinattempts < 10)
            {
                lock (client)
                {
                    Joinattempts++;
                    client.JoinRoom(textBox3.Text);
                    statusStrip1.Invoke(dssd3, new object[] { "Join attempt:" + Joinattempts.ToString() });

                }
            }
            else
            {
                timer1.Stop();
                Joinattempts = 0;
                statusStrip1.Invoke(dssd3, new object[] { "Join attempt:Failed" });
            }
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
        }

        private void unignoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.SelectedItems[0].Checked = false;
            wd.unignoreuser(listView1.SelectedItems[0].Text);
        }

        private void updateStatus(string data)
        {
            statusStrip1.Items[0].Text = data;
        }

        private void wd_AddUser(string data)
        {
            listView1.Invoke(ds, new object[] { data });
        }

        private void wd_DelUser(string data)
        {
            listView1.Invoke(dss, new object[] { data });
        }

        private void wd_KeyUser(string data)
        {
            xtraProgressBar1.Invoke(dssd, new object[] { data });
        }

        private void wd_LevelUser(int data)
        {
            xtraProgressBar1.Invoke(level, new object[] { data });
        }

        #region TabSwitchLogOut
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if((tabControl1.SelectedIndex==0)&&(wd!=null))
            {
                wd.vc_logout();
                wd = null;
            }
        }
        #endregion

        #region Vc Failue Timer
        private void timer2_Tick(object sender, EventArgs e)
        {
            statusStrip1.Invoke(dssd3, new object[] { "VC Failed" });
            if (wd != null)
            {
                wd.Dispose();
                wd = null;
            }
        }
        void timerstart()
        {
            timer2.Start();
        }
        #endregion

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            xtraProgressBar1.Invoke(level2, 0);
            xtraProgressBar1.Invoke(dssd, globaluser);
            keyed = true;
            button3.BackColor = Color.Red;
            wd.VoiceOut();
        }

        private void button3_MouseUp(object sender, MouseEventArgs e)
        {
            xtraProgressBar1.Invoke(dssd, "");
            xtraProgressBar1.Invoke(level, 0);
            xtraProgressBar1.Invoke(level2, 1);
            keyed = false;
            button3.BackColor = button2.BackColor;
            button3.Text = "Key";
            wd.VoiceOff();
        }

     
    }
}
